package cn.vshare.controller;

import cn.vshare.entity.Reply;
import cn.vshare.entity.User;
import cn.vshare.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Administrator
 * @Date: 2018/9/26 17:34
 * @Description:
 */
@RestController
public class ReplyController {

    @Autowired
    @Resource(name = "replyServiceImpl")
    private ReplyService replyService;

    /**
     * 功能描述:处理前台传过来的数据信息
     *
     * @param commentId   回复的commentId
     * @param replyUserId 回复的哪个用户
     * @param date        回复的时间
     * @return
     * @auther 张恒
     * @date 2018/9/27 16:13
     */
    @RequestMapping("/addReply.do")
    public Map addReply(HttpSession session, String commentId, @RequestParam(required = false) String commentUserId, @RequestParam(required = false) String replyUserId, String context, String date) {
        Map map = new HashMap();
        User user = (User) session.getAttribute("USER");
        if (user != null && !commentId.equals("") && !date.equals("")) {
            User toUser = new User();
            if (replyUserId != null && !replyUserId.equals("")) {
                toUser.setId(replyUserId);
            } else if (commentUserId != null && !commentId.equals("")) {
                toUser.setId(commentUserId);
            }
            Reply reply = new Reply();
            reply.setUser(user);
            reply.setToUser(toUser);
            reply.setCreateTime(date);
            Reply newReply = replyService.addReply(user, toUser, commentId, context, date);
            if (newReply != null) {
                map.put("code", 0);
                map.put("msg", "评论成功!ヾ(◍°∇°◍)ﾉﾞ");
                map.put("newReply", newReply);
            } else {
                map.put("code", 1);
                map.put("msg", "评论失败!o(╥﹏╥)o");
            }
        } else {
            map.put("code", 1);
            map.put("msg", "评论失败!o(╥﹏╥)o");
        }
        return map;
    }

    @RequestMapping("/updateReplyAddLike.do")
    public Map updateReplyAddLike(HttpSession session, String replyId) {
        Map map = new HashMap();
        User user = (User) session.getAttribute("USER");
        try {
            if (replyService.updateReplyAddLike(user, replyId)) {
                Integer parseCount = replyService.queryReplyById(replyId).getParseCount();
                map.put("code", 0);
                map.put("msg", "点赞成功");
                map.put("parseCount", parseCount);
            } else {
                map.put("code", 1);
                map.put("msg", "点赞失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/updateReplyCancelLike.do")
    public Map updateReplyCancelLike(HttpSession session, String replyId) {
        Map map = new HashMap();
        User user = (User) session.getAttribute("USER");
        try {
            if (replyService.updateReplyCancelLike(user, replyId)) {
                Integer parseCount = replyService.queryReplyById(replyId).getParseCount();
                map.put("code", 0);
                map.put("msg", "点赞成功");
                map.put("parseCount", parseCount);
            } else {
                map.put("code", 1);
                map.put("msg", "点赞失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/queryReplyIfLike.do")
    public String queryReplyIfLike(HttpSession session, String replyId) {
        User user = (User) session.getAttribute("USER");
        if (replyService.queryReplyIfLike(replyId, user) == 1) {
            return "true";
        }
        return "";
    }
}
